
#include <material/editorprogram.h>
#include <material/editorshader.h>
#include <session.h>

EditorProgram::EditorProgram()
{
  CE_OBJECT_CONSTR
  _program = Session::Get()->GetDevice()->CreateProgram();
  if (_program)
    {
      _program->AddRef();
    }
}

EditorProgram::~EditorProgram()
{
  CE_UNSET(_program);
}

void EditorProgram::ClearShaders()
{
  _program->ClearShaders();
}

void EditorProgram::AttachShader(iShader *shader)
{
  EditorShader* editorShader = static_cast<EditorShader*>(shader);
  _program->AttachShader(editorShader->GetSource());

  _shaders.push_back(editorShader);
  editorShader->AddRef();
}

void EditorProgram::DetachShader(iShader *shader)
{
  EditorShader* editorShader = static_cast<EditorShader*>(shader);
  _program->DetachShader(editorShader->GetSource());

  for (std::vector<EditorShader*>::iterator it = _shaders.begin();
       it != _shaders.end();
       ++it)
    {
      EditorShader* es = *it;
      if (es == editorShader)
        {
          es->Release();
          _shaders.erase(it);
          return;
        }
    }
}

bool EditorProgram::Link()
{
  return _program->Link();
}

std::string EditorProgram::GetLinkInfoLog() const
{
  return _program->GetLinkInfoLog();
}

unsigned EditorProgram::GetNumberOfShaders() const
{
  return _shaders.size();
}

EditorShader* EditorProgram::GetShader(unsigned idx)
{
  return _shaders[idx];
}

void EditorProgram::Bind()
{
  _program->Bind();
}

void EditorProgram::RegisterSemantic(const ceProgramParameterName &name, const std::string &parameterName)
{
  _program->RegisterSemantic(name, parameterName);
}

void EditorProgram::RegisterSemantic(ceProgramParameterBinding binding, const std::string &parameterName)
{
  _program->RegisterSemantic(binding, parameterName);
}

iProgramParameter* EditorProgram::GetParameter(const ceProgramParameterName &name)
{
  return _program->GetParameter(name);
}

iProgramParameter* EditorProgram::GetParameter(ceProgramParameterBinding binding)
{
  return _program->GetParameter(binding);
}
